﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TEG.Models;

namespace TEG.Controllers
{ 
    public class VehiculosController : Controller
    {
        private ArlandriaEntities db = new ArlandriaEntities();

        //
        // GET: /Vehiculos/

        public ViewResult Index()
        {
            var vehiculo = db.Vehiculo.Include("Usuario").Include("VehiculoEdicion");
            return View(vehiculo.ToList());
        }

        //
        // GET: /Vehiculos/Details/5

        public ViewResult Details(int id)
        {
            Vehiculo vehiculo = db.Vehiculo.Single(v => v.Id == id);
            return View(vehiculo);
        }

        //
        // GET: /Vehiculos/Create

        public ActionResult Create()
        {
            ViewBag.IdUsuario = new SelectList(db.Usuario, "Id", "Nombre");
            ViewBag.IdVehiculoEdicion = new SelectList(db.VehiculoEdicion, "Id", "Nombre");
            return View();
        } 

        //
        // POST: /Vehiculos/Create

        [HttpPost]
        public ActionResult Create(Vehiculo vehiculo)
        {
            if (ModelState.IsValid)
            {
                db.Vehiculo.AddObject(vehiculo);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.IdUsuario = new SelectList(db.Usuario, "Id", "Nombre", vehiculo.IdUsuario);
            ViewBag.IdVehiculoEdicion = new SelectList(db.VehiculoEdicion, "Id", "Nombre", vehiculo.IdVehiculoEdicion);
            return View(vehiculo);
        }
        
        //
        // GET: /Vehiculos/Edit/5
 
        public ActionResult Edit(int id)
        {
            Vehiculo vehiculo = db.Vehiculo.Single(v => v.Id == id);
            ViewBag.IdUsuario = new SelectList(db.Usuario, "Id", "Nombre", vehiculo.IdUsuario);
            ViewBag.IdVehiculoEdicion = new SelectList(db.VehiculoEdicion, "Id", "Nombre", vehiculo.IdVehiculoEdicion);
            return View(vehiculo);
        }

        //
        // POST: /Vehiculos/Edit/5

        [HttpPost]
        public ActionResult Edit(Vehiculo vehiculo)
        {
            if (ModelState.IsValid)
            {
                db.Vehiculo.Attach(vehiculo);
                db.ObjectStateManager.ChangeObjectState(vehiculo, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.IdUsuario = new SelectList(db.Usuario, "Id", "Nombre", vehiculo.IdUsuario);
            ViewBag.IdVehiculoEdicion = new SelectList(db.VehiculoEdicion, "Id", "Nombre", vehiculo.IdVehiculoEdicion);
            return View(vehiculo);
        }

        //
        // GET: /Vehiculos/Delete/5
 
        public ActionResult Delete(int id)
        {
            Vehiculo vehiculo = db.Vehiculo.Single(v => v.Id == id);
            return View(vehiculo);
        }

        //
        // POST: /Vehiculos/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Vehiculo vehiculo = db.Vehiculo.Single(v => v.Id == id);
            db.Vehiculo.DeleteObject(vehiculo);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}